package com.xn.myyxcampus.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.util.LogUtils;
import com.xn.myyxcampus.MyApplication;
import com.xn.myyxcampus.R;
import com.xn.myyxcampus.bean.NewsList;
import com.xn.myyxcampus.bean.NewsListItem;
import com.xn.myyxcampus.http.URLs;
import com.xn.myyxcampus.utils.TimeUtil;

public class NewsListViewAdapter extends BaseAdapter {

	private Context context = MyApplication.getContext();
	private String bitmapCache = context.getExternalCacheDir() + "/tracy/News/";
	private NewsList list = null;

	private BitmapUtils bitmapUtils = new BitmapUtils(context, bitmapCache);

	public void loadData(NewsList list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (null == list) {
			return 0;
		} else {
			return list.getList().size();
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (null == list) {
			return null;
		} else {
			ViewHolder holder = new ViewHolder();
			NewsListItem item = list.getList().get(position);
			if (null == convertView) {
				convertView = LayoutInflater.from(context).inflate(
						R.layout.news_list_item, null);
				holder.iv_newsListPic = (ImageView) convertView
						.findViewById(R.id.iv_newsListPic);
				holder.tv_newsListTitle = (TextView) convertView
						.findViewById(R.id.tv_newsListTitle);
				holder.tv_newsListCount = (TextView) convertView
						.findViewById(R.id.tv_newsListCount);
				holder.tv_newsListTime = (TextView) convertView
						.findViewById(R.id.tv_newsListTime);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			LogUtils.i(item.getTitle());
			holder.tv_newsListTitle.setText(item.getTitle());
			LogUtils.i(item.getClick());
			holder.tv_newsListCount.setText(item.getClick());
			LogUtils.i(TimeUtil.getCurrrentTime(Long.parseLong(item.getTime())));
			holder.tv_newsListTime.setText(TimeUtil.getCurrrentTime(Long
					.parseLong(item.getTime())));
			String imageUrl = URLs.NEWS_LIST_PHOTO
					+ ((String) item.getNewspic()).replace('+', '/');
			bitmapUtils.display(holder.iv_newsListPic, imageUrl);
			// bitmapUtils.dis
			return convertView;
		}
	}

	class ViewHolder {
		public ImageView iv_newsListPic;
		public TextView tv_newsListTitle;
		public TextView tv_newsListTime;
		public TextView tv_newsListCount;
	}

}
